package com.situ.day6;

import org.junit.Test;

import java.util.Scanner;

public class HomeWork {

    //1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
    //求出上面数组中0-9分别出现的次数
    @Test
    public void test1() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int count0 = 0;
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        for (int i = 0; i < scores.length; i++) {
            /*if (scores[i] == 0) {
                count0++;
            } else if (scores[i] == 1) {
                count1++;
            } else if (scores[i] == 2) {
                count2++;
            }*/
            switch (scores[i]) {
                case 0:
                    count0++;
                    break;
                case 1:
                    count1++;
                    break;
                case 2:
                    count2++;
                    break;
            }
        }
        System.out.println("0出现次数：" + count0);
        System.out.println("1出现次数：" + count1);
        System.out.println("2出现次数：" + count2);
    }

    @Test
    public void test5() {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {
            switch (scores[i]) {
                case 0:
                    counts[0]++;
                    break;
                case 1:
                    counts[1]++;
                    break;
                case 2:
                    counts[2]++;
                    break;
            }
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "出现次数：" + counts[i]);
        }
    }

    @Test
    public void test2() {
        int[] scores = {0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9};
        int[] counts = new int[10];
        for (int i = 0; i <= 9; i++) {
            //i=0 统计0出现次数
            for (int j = 0; j < scores.length; j++) {
                if (scores[j] == i) {
                    counts[i]++;
                }
            }
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "出现次数：" + counts[i]);
        }
    }

    @Test
    public void test22() {
        int[] scores = {0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9};
        int[] counts = new int[10];
        for (int i = 0; i < scores.length; i++) {
            counts[scores[i]]++;
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "出现次数：" + counts[i]);
        }
    }

    //2、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
    //要求求出其中的奇数个数和偶数个数。
    public static void main(String[] args) {
        int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int oddCount = 0;
        int evenCount = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        System.out.println("奇数个数：" + oddCount);
        System.out.println("偶数个数：" + evenCount);
    }

    //12、题目：一个任意一个字符串，判断它是不是回文。例如12321是回文数，个位与万位相同，十位与千位相同。
    @Test
    public void test889() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串： ");
        String str = scanner.next();
        //String str = "abcba";
        char[] array = str.toCharArray();
        //初始认为是回文，接下来遍历看能不能找到一个反例
        //最后遍历完了都没有找到反例，代表是回文
        //boolean flag = true;
        boolean isHuiWen = true;
        for (int i = 0; i < array.length / 2; i++) {
            if (array[i] != array[array.length - i - 1]) {
                isHuiWen = false;
                //找到一个反例，就证明一定不是回文，不需要再往后判断
                break;
            }
        }
        if (isHuiWen) {
            System.out.println("是回文");
        } else {
            System.out.println("不是回文");
        }
    }

    //13、输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。
    @Test
    public void test78() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = scanner.nextLine();
        //"dsfhi 34**DSD [34"
        char[] array = str.toCharArray();
        int letterCount = 0;
        int spaceCount = 0;
        int digitCount = 0;
        int otherCount = 0;
        for (int i = 0; i < array.length; i++) {
            if ((array[i] >= 'A' && array[i] <= 'Z') || (array[i] >= 'a' && array[i] <= 'z')) {
                letterCount++;
            } else if (array[i] == ' ') {
                spaceCount++;
            } else if (array[i] >= '0' && array[i] <= '9') {
                digitCount++;
            } else {
                otherCount++;
            }
        }
        System.out.println("字母出现次数：" + letterCount);
        System.out.println("空格出现次数：" + spaceCount);
        System.out.println("数字出现次数：" + digitCount);
        System.out.println("其他出现次数：" + otherCount);
    }


}
